package com.cgbl.cg.vm.mine

import android.app.Application
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import api.UserServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.configs.PrefsManager
import com.robot.base.model.UserInfoBean
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse
import me.goldze.mvvmhabit.utils.StringUtils

class MineViewModel(application: Application) : GLBaseViewModel(application) {

    val uc = UiChangeEvent()

    val clickPersonalProfileCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Mine.A_MINE_PERSONAL_PAGE)
    })

    val clickPersonalSignCommand = BindingCommand<Any>(BindingAction {
//        ARouterUtil.goToActivity(AppConstants.Router.Mine.A_MINE_CAREER_PATH)
        uc.showSignPop.postValue("")
    })

    val clickExchangeCodePopCommand = BindingCommand<Any>(BindingAction {
        uc.showExchangeCodePop.postValue("")
    })

    val clickPersonalCourseCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Mine.A_MINE_COURSE_CENTER)
    })

    val clickPersonalProjectCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Mine.A_MINE_PROJECT_CENTER)
    })

    val clickPersonalResourceCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Mine.A_MINE_RESOURCE_CENTER)
    })

    val clickPersonalWorksCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Mine.A_MINE_PERSONAL_WORKS)
    })

    val clickPersonalCouponCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Mine.A_MINE_PERSONAL_COUPON)
    })

    val clickWithdrawCenterCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Mine.A_MINE_WITHDRAW_CENTER)
    })

    val clickWorksDataCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Mine.A_MINE_PERSONAL_WORKS_DATA)
    })

    val clickShoppingCarCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Mine.A_MINE_SHOPPING_CAR)
    })

    val clickOfflineCacheCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Mine.A_MINE_OFFLINE_CACHE)
    })

    val clickSettingCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Mine.A_MINE_SETTING)
    })

    val clickFeedbackCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Mine.A_MINE_FEEDBACK)
    })

    val clickVipCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Mine.A_MINE_OPEN_VIP)
    })

    val userAvatar = ObservableField<String>()

    val nickNameField = ObservableField<String>()

    val engineerStrField = ObservableField<String>()

    val hasEngineerField = ObservableBoolean()

    val overallIntegralField = ObservableField<String>()

    val fansCountField = ObservableField<String>()

    val followCountField = ObservableField<String>()

    val isVipField = ObservableBoolean()

    val vipTimeField = ObservableField<String>()

    fun refreshUserInfo() {
        UserServiceFactory.userInfo()
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<UserInfoBean>>(this) {
                override fun onNext(t: BaseResponse<UserInfoBean>) {
                    PrefsManager.saveUserInfo(t.data)
                    refreshUserInfoData(t.data)
                }
            })
    }

    private fun refreshUserInfoData(userInfoBean: UserInfoBean) {
        nickNameField.set(userInfoBean.nickName)
        engineerStrField.set(userInfoBean.engineer)
        hasEngineerField.set(!StringUtils.isEmpty(userInfoBean.engineer))
        overallIntegralField.set(userInfoBean.overallIntegral)
        fansCountField.set(userInfoBean.fansCount)
        followCountField.set(userInfoBean.followCount)
        isVipField.set(userInfoBean.vipType == "1")
        vipTimeField.set("2024年12月24日到期")
        userAvatar.set(userInfoBean.head)
    }

    class UiChangeEvent {
        val showSignPop: SingleLiveEvent<String> = SingleLiveEvent()

        val showExchangeCodePop: SingleLiveEvent<String> = SingleLiveEvent()
    }
}